package com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.SmsEnrollmentManagementStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.SmsEnrollmentManagementTask;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.SmsEnrollmentManagementTeacherInfo;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementTeacherInfoReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.resp.IsEnrollmentTeacherInfoResp;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.resp.SmsEnrollmentManagementTeacherInfoResp;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.resp.SmsEnrollmentManagementTeacherTaskResp;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.mapper.SmsEnrollmentManagementStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.mapper.SmsEnrollmentManagementTaskMapper;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.mapper.SmsEnrollmentManagementTeacherInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.SmsEnrollmentManagementTeacherInfoService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.constant.CommonConstant.ENROLLMENT_QR_HTTPS;

/**
 * <p>
 * 招生管理招生教师 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@Service
public class SmsEnrollmentManagementTeacherInfoServiceImpl extends ServiceImpl<SmsEnrollmentManagementTeacherInfoMapper, SmsEnrollmentManagementTeacherInfo> implements SmsEnrollmentManagementTeacherInfoService {
    @Resource
    private SmsEnrollmentManagementTeacherInfoMapper smsEnrollmentManagementTeacherInfoMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private SmsEnrollmentManagementTaskMapper smsEnrollmentManagementTaskMapper;
    @Resource
    private SmsEnrollmentManagementStudentInfoMapper smsEnrollmentManagementStudentInfoMapper;

    @Override
    public ResponseResult addEnrollmentManagementTeacherInfo(SmsEnrollmentManagementTeacherInfoReq teacherInfoReq) {
        if (isNullOrEmpty(teacherInfoReq.getUserId(), teacherInfoReq.getTeacherName(),
                teacherInfoReq.getTeacherSex(), teacherInfoReq.getTeacherContact())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsUser smsUser = smsUserMapper.selectById(teacherInfoReq.getUserId());
        if (smsUser == null) {
            return CommonResult.failed(CommonCodeEnum.USER_NOT_EXIST);
        }

        List<SmsEnrollmentManagementTeacherInfo> list = smsEnrollmentManagementTeacherInfoMapper.selectList(new LambdaQueryWrapper<SmsEnrollmentManagementTeacherInfo>()
                .eq(SmsEnrollmentManagementTeacherInfo::getUserId,teacherInfoReq.getUserId()));
        if (list != null && list.size() > 0){
            return CommonResult.failed(CommonCodeEnum.ENROLLMENT_MANAGEMENT_TEACHER_USER_EXIST);
        }

        SmsEnrollmentManagementTeacherInfo addTeacherInfo = new SmsEnrollmentManagementTeacherInfo();
        BeanUtils.copyProperties(teacherInfoReq, addTeacherInfo);
        smsEnrollmentManagementTeacherInfoMapper.insert(addTeacherInfo);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteEnrollmentManagementTeacherInfo(SmsEnrollmentManagementTeacherInfoReq teacherInfoReq) {
        if (isNullOrEmpty(teacherInfoReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        smsEnrollmentManagementTeacherInfoMapper.deleteById(teacherInfoReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateEnrollmentManagementTeacherInfo(SmsEnrollmentManagementTeacherInfoReq teacherInfoReq) {
        if (isNullOrEmpty(teacherInfoReq.getId(), teacherInfoReq.getTeacherSex(),
                teacherInfoReq.getTeacherContact(), teacherInfoReq.getTeacherName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsEnrollmentManagementTeacherInfo updateTeacherInfo = new SmsEnrollmentManagementTeacherInfo();
        BeanUtils.copyProperties(teacherInfoReq, updateTeacherInfo);
        smsEnrollmentManagementTeacherInfoMapper.updateById(updateTeacherInfo);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getEnrollmentManagementTeacherInfoList(String searchStr, Integer pageNum, Integer pageSize) {
        QueryWrapper<SmsEnrollmentManagementTeacherInfo> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.lambda().and(wrapper -> {
                wrapper.like(SmsEnrollmentManagementTeacherInfo::getTeacherName, searchStr)
                        .or()
                        .like(SmsEnrollmentManagementTeacherInfo::getTeacherContact, searchStr)
                        .or()
                        .like(SmsEnrollmentManagementTeacherInfo::getTeacherEmail, searchStr);
            });
        }
        queryWrapper.lambda().orderByDesc(SmsEnrollmentManagementTeacherInfo::getCreateTime);
        Page<SmsEnrollmentManagementTeacherInfo> page = new Page<>(pageNum, pageSize);
        smsEnrollmentManagementTeacherInfoMapper.selectPage(page, queryWrapper);

        List<SmsEnrollmentManagementTeacherInfoResp> list = page.getRecords()
                .stream()
                .map(teacherInfo -> {
                    SmsEnrollmentManagementTeacherInfoResp teacherInfoResp = new SmsEnrollmentManagementTeacherInfoResp();
                    BeanUtils.copyProperties(teacherInfo, teacherInfoResp);

                    String teacherQR = ENROLLMENT_QR_HTTPS + "type=enrollment&" +"teacherId="+teacherInfo.getId();
                    teacherInfoResp.setTeacherQR(teacherQR);
                    return teacherInfoResp;
                })
                .collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list, total);
    }

    @Override
    public ResponseResult getEnrollmentManagementTeacherInfoDetail(String teacherId, Integer pageNum, Integer pageSize) {
        SmsEnrollmentManagementTeacherInfo teacherInfo = smsEnrollmentManagementTeacherInfoMapper.selectById(teacherId);
        if (teacherInfo == null) {
            return CommonResult.failed(CommonCodeEnum.TEACHER_NOT_EXIST);
        }
        SmsEnrollmentManagementTeacherInfoResp teacherInfoResp = new SmsEnrollmentManagementTeacherInfoResp();
        BeanUtils.copyProperties(teacherInfo, teacherInfoResp);
        String teacherQR = ENROLLMENT_QR_HTTPS + "type=enrollment&" +"teacherId="+teacherId;
        teacherInfoResp.setTeacherQR(teacherQR);

        QueryWrapper<SmsEnrollmentManagementTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SmsEnrollmentManagementTask::getTaskStatus)
                .orderByDesc(SmsEnrollmentManagementTask::getCreateTime);
        Page<SmsEnrollmentManagementTask> page = new Page<>();
        smsEnrollmentManagementTaskMapper.selectPage(page, queryWrapper);
        List<SmsEnrollmentManagementTeacherTaskResp> list = page.getRecords()
                .stream()
                .map(task->{
                    SmsEnrollmentManagementTeacherTaskResp taskResp = new SmsEnrollmentManagementTeacherTaskResp();
                    BeanUtils.copyProperties(task,taskResp);

                    Integer count = smsEnrollmentManagementStudentInfoMapper.selectCount(new LambdaQueryWrapper<SmsEnrollmentManagementStudentInfo>()
                            .eq(SmsEnrollmentManagementStudentInfo::getEnrollmentTaskId,task.getId())
                            .eq(SmsEnrollmentManagementStudentInfo::getEnrollmentTeacher,teacherId));
                    taskResp.setTeacherTaskNum(count);
                    return taskResp;
                })
                .collect(Collectors.toList());
        teacherInfoResp.setTeacherTaskList(list);
        int total = (int) page.getTotal();
        return CommonResult.success(teacherInfoResp,total);
    }

    @Override
    public ResponseResult getTeacherIdByUserId(String user_id) {
        SmsEnrollmentManagementTeacherInfo teacherInfo = smsEnrollmentManagementTeacherInfoMapper.selectOne(new LambdaQueryWrapper<SmsEnrollmentManagementTeacherInfo>()
                .eq(SmsEnrollmentManagementTeacherInfo::getUserId,user_id));
        IsEnrollmentTeacherInfoResp isEnrollmentTeacherInfoResp = new IsEnrollmentTeacherInfoResp();
        if (teacherInfo == null){
            isEnrollmentTeacherInfoResp.setEnrollmentTeacher(false);
        }else{
            isEnrollmentTeacherInfoResp.setEnrollmentTeacher(true);
            isEnrollmentTeacherInfoResp.setTeacherId(teacherInfo.getId());
        }
        return CommonResult.success(isEnrollmentTeacherInfoResp);
    }
}
